package com.example.jetpackcomposebasic

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
 * 给 Modifier 对象创建一个扩展函数
 * 让文字的baseline距离top有 firstBaseLineToTop.dp
 * */
fun Modifier.firstBaseLineToTop(firstBaseLineToTop: Dp): Modifier {
    //
    return this.then(layout { measurable, constrains ->

        // 测量元素
        val placeable = measurable.measure(constrains)

        // 获取测量之后，元素的基线值 baseline
        val firstBaseLine = placeable[FirstBaseline]

        val placeableY = firstBaseLineToTop.roundToPx() - firstBaseLine

        val newHeight = placeable.height + placeableY

        layout(placeable.width, newHeight) {

            placeable.placeRelative(0, placeableY)
        }
    })
}

/**
 * 使用扩展函数，修改自定义布局
 *
 */
@Composable
fun BaseLineText() {
    Text(
        text = "天气真好的基线距离top有34dp",
        modifier = Modifier
            .firstBaseLineToTop(104.dp)
            .background(Color.Green)


    )
}